﻿using System;
using System.Text;
using System.Collections.Generic;
using Amazon;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using Newtonsoft.Json;
using Rezene.Web.Business;
using Rezene.Web.Business.Amazon;
using Rezene.Web.Business.Amazon.Entities;

namespace Rezene.Web.Tests
{
    /// <summary>
    /// Summary description for UnitTest1
    /// </summary>
    [TestClass]
    public class AmazonBusinessTests
    {
        public IConfigurationRepository ConfigurationRepository{ get; set; }
        public IAmazonRepository BucketRepository { get; set; }
        public IAmazonFactory AmazonFactory { get; set; }

        public AmazonBusinessTests()
        {
            var mock = new Mock<IConfigurationRepository>();
            mock.Setup(c => c.GetAmazonAccessKey()).Returns("AKIAJIJMYHZHPUHJWH4A");
            mock.Setup(c => c.GetAmazonSecretKey()).Returns("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");
            mock.Setup(c => c.GetAmazonContainerBucket()).Returns("rezene-gallery");
            mock.Setup(c => c.GetAmazonEndPoint()).Returns(RegionEndpoint.USWest2);
            ConfigurationRepository = mock.Object;
            AmazonFactory = new AmazonFactory();
            BucketRepository = new AmazonRepository(AmazonFactory);            
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void MyTestInitialize()
        {
            BucketRepository.Init(ConfigurationRepository.GetAmazonAccessKey(), ConfigurationRepository.GetAmazonSecretKey(), ConfigurationRepository.GetAmazonEndPoint(), ConfigurationRepository.GetAmazonContainerBucket());
        }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void VerifyWeCanReachBucket()
        {            
            var galleries = BucketRepository.GetGalleries("");
            Assert.IsTrue(galleries != null);            
        }

        [TestMethod]
        public void VerifyGalleryJsonSerializationWorks()
        {
            S3Gallery gal = AmazonFactory.CreateGallery(ConfigurationRepository.GetAmazonContainerBucket(),"Test", "some/folders/", "galerrytitle", "descriptiopn", true);
            var jsonString = JsonConvert.SerializeObject(gal);
            Assert.IsFalse(string.IsNullOrEmpty(jsonString));
            var newGal = AmazonFactory.CreateGalleryFromJson(jsonString);
            Assert.IsTrue(gal.Name == newGal.Name);
        }

        [TestMethod]
        public void VerifySaveGalleryWorks()
        {
            S3Gallery gal = AmazonFactory.CreateGallery(ConfigurationRepository.GetAmazonContainerBucket(),"Galery-1", "", "galerrytitle", "descriptiopn", true);            
            var result = BucketRepository.SaveGalleryInfo(gal);
            Assert.IsTrue(result);
        }

        [TestMethod]
        public void VerifyReadGalleryInfoWorks()
        {
            var gal = BucketRepository.GetGalleryInfo("Galery-1");
            Assert.IsNotNull(gal);
        }

        [TestMethod]
        public void VerifySaveAndGetPhotoInfoWorks()
        {
            S3PhotoObject photo = AmazonFactory.CreatePhotoObject(ConfigurationRepository.GetAmazonContainerBucket(),"doga.jpg", "Galery-1", "Photo Title", "Photo Description");
            var result = BucketRepository.SavePhotoInfo(photo);
            Assert.IsTrue(result);
            var obj = BucketRepository.GetPhotoInfo("Galery-1/doga.jpg");
            Assert.IsTrue(obj.Title == photo.Title);
        }

        [TestMethod]
        public void VerifyWeCanListPhotos()
        {
            BucketRepository.AllowedExtensions = ".jpg,.jpeg,.png";
            var photos = BucketRepository.GetPhotos("Galery-1");
            Assert.IsNotNull(photos);
            Assert.IsTrue(photos.Count > 0);
        }
    }
}
